# include <bits/stdc++.h>
using namespace std;
long long x, y, l, r;
vector<long long> a, b, ans, ans1;
inline void get_ans() {
    a.push_back(1);
    b.push_back(1);
    long long num = 1;
    while (num <= min(r, (long long)1e18 / x)) {
        num *= x;
        a.push_back(num);
    }
    num = 1;
    while (num <= min(r, (long long)1e18 / y)) {
        num *= y;
        b.push_back(num);
    }
    for(int i = 0; i < a.size(); i++) {
        for(int j = 0; j < b.size(); j++) {
            static long long tmp;
            tmp = a[i] + b[j];
            if(tmp > r || tmp < l) continue;
            ans.push_back(tmp);
        }
    }
    ans.push_back(l - 1);
    ans.push_back(r + 1);
    sort(ans.begin(), ans.end());
    ans1.push_back(ans[0]);
    for(int i = 1; i < ans.size(); i++) { 
        if(ans[i] == ans1.back()) continue;
        else ans1.push_back(ans[i]);
    }
}

int main() { 
# ifndef LOCAL 
    freopen("TGA.in", "r", stdin);
    freopen("TGA.out", "w", stdout);
# else 
    freopen("in", "r", stdin);
# endif
    scanf("%lld%lld%lld%lld", &x, &y, &l, &r);
    get_ans();
    //for(int i = 0; i < ans1.size(); i++) printf("%d ", ans1[i]);
    long long lt = 0;
    long long s = ans1[0], t;
    for(int i = 1; i < ans1.size(); i++) { 
        t = ans1[i];
        lt = max(lt, t - s - 1);
        s = t;
    }
    printf("%lld\n", lt);
}
